package com.ruoyi.easyfame.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.easyfame.service.IExpertService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.easyfame.mapper.AppointmentMapper;
import com.ruoyi.easyfame.domain.Appointment;
import com.ruoyi.easyfame.service.IAppointmentService;

/**
 * 预约申请Service业务层处理
 * 
 * @author xhb
 * @date 2025-08-20
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService 
{
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询预约申请
     * 
     * @param id 预约申请主键
     * @return 预约申请
     */
    @Override
    public Appointment selectAppointmentById(Long id)
    {
        Appointment appointment = appointmentMapper.selectAppointmentById(id);
        if (appointment != null) {
            // 获取农户姓名
            SysUser farmer = userService.selectUserById(appointment.getFarmerId());
            if (farmer != null) {
                appointment.setFarmerName(farmer.getNickName());
            }

            // 获取专家姓名
            SysUser expert = userService.selectUserById(appointment.getExpertId());
            if (expert != null) {
                appointment.setExpertName(expert.getNickName());
            }
        }


        return appointment;
    }

    /**
     * 查询预约申请列表
     * 
     * @param appointment 预约申请
     * @return 预约申请
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment)
    {
        List<Appointment> list = appointmentMapper.selectAppointmentList(appointment);

        // 关联查询用户信息
        for (Appointment appt : list) {
            // 查询农户姓名
            SysUser farmer = userService.selectUserById(appt.getFarmerId());
            if (farmer != null) {
                appt.setFarmerName(farmer.getNickName());
            }

            // 查询专家姓名
            SysUser expert = userService.selectUserById(appt.getExpertId());
            if (expert != null) {
                appt.setExpertName(expert.getNickName());
            }
        }

        return list;
    }

    /**
     * 新增预约申请
     * 
     * @param appointment 预约申请
     * @return 结果
     */
    @Override
    public int insertAppointment(Appointment appointment)
    {
        appointment.setCreateTime(DateUtils.getNowDate());

        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改预约申请
     * 
     * @param appointment 预约申请
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment)
    {
        appointment.setUpdateTime(DateUtils.getNowDate());
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除预约申请
     * 
     * @param ids 需要删除的预约申请主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByIds(Long[] ids)
    {
        return appointmentMapper.deleteAppointmentByIds(ids);
    }

    /**
     * 删除预约申请信息
     * 
     * @param id 预约申请主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentById(Long id)
    {
        return appointmentMapper.deleteAppointmentById(id);
    }
}
